package interfaces;


import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Vector;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import entidades.Licencia;
import entidades.Titular;
import gestores.GestorLicencia;
import gestores.GestorTitular;

/**
 * Profile Controller.
 */
public class BusquedaDeLicenciaController extends AnchorPane implements Initializable {

  
    private Main application;
    private Stage util;
    private EmitirLicenciaController emitirlicenciacontroller1;
    public GestorTitular gt= new GestorTitular();
    public Vector<Licencia> licencias= new Vector<Licencia>();
    GestorLicencia gestor= new GestorLicencia();
    GestorTitular gestortitular= new GestorTitular();
    ImprimirLicenciaController imprimirlicenciacontroller;
  /*  @FXML
    private DatePicker datepicker;*/
    
    @FXML
    public Button cancelar;
   
    @FXML private TableView<EntradaTabla> tableView;
    
    @FXML private TextField nrolicencia;
    @FXML private TextField dnititular;
   
    //@FXML
    //public TableColumn<Person, String> n;
    
    //public TableColumn radio;
    
   
   /* @FXML
    public TableColumn<EntradaTabla, String> columna1;
    
    @FXML
    public TableColumn<EntradaTabla, String> columna2;
    
    @FXML
    public TableColumn<EntradaTabla, String> columna3;
    
    @FXML
    public TableColumn<EntradaTabla, String> columna4;
    
    @FXML
    public TableColumn<EntradaTabla, RadioButton> columna0;*/
    
   
    
    
    ObservableList<EntradaTabla> data;
    
    @FXML
    public void setApp(Main main, ImprimirLicenciaController imprimirlicenciacontroller){
    	this.application=main;
   
    	this.imprimirlicenciacontroller= imprimirlicenciacontroller;
    	data = FXCollections.observableArrayList();
    	tableView.setItems(data);
    	TableColumn<EntradaTabla, String> col1= new TableColumn<EntradaTabla, String>("Nro de licencia");
    	col1.setCellValueFactory(
    	            new PropertyValueFactory<EntradaTabla,String>("firstName")
    	        );
    	
    	TableColumn<EntradaTabla, String> col2= new TableColumn<EntradaTabla, String>("Nombre y Apellido");
    	col2.setCellValueFactory(
    	            new PropertyValueFactory<EntradaTabla,String>("lastName")
    	        );
    	TableColumn<EntradaTabla, String> col3= new TableColumn<EntradaTabla, String>("Nro de documento");
    	col3.setCellValueFactory(
    	            new PropertyValueFactory<EntradaTabla,String>("TipoDoc")
    	        );
    	TableColumn<EntradaTabla, String> col4= new TableColumn<EntradaTabla, String>("Clase");
    	col4.setCellValueFactory(
    	            new PropertyValueFactory<EntradaTabla,String>("nroDoc")
    	        );
    	     
    	        tableView.getColumns().addAll(col1,col2,col3,col4);
    	        col1.setPrefWidth(150);
    	        col2.setPrefWidth(170);
    	        col3.setPrefWidth(150);
    	        col4.setPrefWidth(179);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
   
/*datepicker.setPromptText("mm/dd/yyyy");*/
    	//nrodoc.addEventFilter(KeyEvent.KEY_PRESSED, null )
    	
    	/*nrodoc.setOnKeyPressed(new EventHandler<KeyEvent>() {
    		public void handle(KeyEvent ke) {
    		System.out.println("Key Pressed: " + ke.getText());
    		}
    		});*/
    	
    	
   
    }
 
  
    
    public void cancelar(ActionEvent event) {
    	application.stage.getScene().getRoot().setEffect(null);
    	application.cerrarModal();
    }
    
    public void aceptar(ActionEvent event) {
       
    	
    	
    	if (tableView.getSelectionModel().getSelectedItem()!=null)
    	{
    	application.stage.getScene().getRoot().setEffect(null);
    	Licencia l=licencias.elementAt((tableView.getSelectionModel().getSelectedIndex()));
    	application.cerrarModal();
    	application.stage.getScene().getRoot().setEffect(null);
    	
      	java.util.Date date = new java.util.Date(); 
		java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("dd/MM/yyyy");
		imprimirlicenciacontroller.licencia=l;
		imprimirlicenciacontroller.textfield1.setText(l.idLicencia+"");
		imprimirlicenciacontroller.textfield2.setText(l.titular.nombre+" "+l.titular.apellido);
		imprimirlicenciacontroller.textfield3.setText(l.clase.idClase);
		imprimirlicenciacontroller.textfield4.setText(l.vencimiento+"");
    	}
    	
    	else
    	{
    		/*error.setVisible(true);*/
    	}
      	
     }
     
 
    
    

    
    public void buscar(ActionEvent event) {
       
    	
    	data.clear();
    	licencias=gestor.buscarLicencia(nrolicencia.getText(), dnititular.getText());
    	data.addAll(convertir(licencias));
    	
     }
    
    public Vector<EntradaTabla> convertir(Vector<Licencia> licencias){   	
    	Vector<EntradaTabla> v= new Vector<EntradaTabla>();
    	for (int i=0;i<licencias.size();i++)
    	{
    		v.add(new EntradaTabla(""+licencias.elementAt(i).idLicencia,licencias.elementAt(i).titular.nombre+" "+licencias.elementAt(i).titular.apellido, licencias.elementAt(i).titular.nroDoc+"" , String.valueOf(licencias.elementAt(i).clase.idClase)));
    		
    	}
		return v;
    }
    
}
